﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathLib;

namespace Demog {
    public partial class SimulatedPopulation: Form {

       private Matrix[] populations;
       private Matrix[] populationMatrixes;
       private int period;
       private int groupCount;
       private int periodCount;
       private double P0;
       private double Vnu, Ut;
       private Matrix x, xx;

       public SimulatedPopulation(Matrix[] P, Matrix[] populationMatrixes, int period, int groupCount) {

            this.populations = P;
            this.populationMatrixes = populationMatrixes;
            this.period = period;
            this.groupCount = groupCount;
            InitializeComponent();
        }


       private void PrintMatrix(DataGridView dgv, Matrix m) {
           DataTable table = CreateTable(m.NoRows, m.NoCols);
           for (int i = 0; i < m.NoRows; i++) {
               DataRow dr = table.Rows[i];
               for (int j = 0; j < m.NoCols; j++)
                   dr[j] = Math.Round(m[i, j],5);
           }

           dgv.DataSource = table;
       }

       private void PrintMatrixRound(DataGridView dgv, Matrix m) {
           DataTable table = CreateTable(m.NoRows, m.NoCols);
           for (int i = 0; i < m.NoRows; i++) {
               DataRow dr = table.Rows[i];
               for (int j = 0; j < m.NoCols; j++)
                   dr[j] = Math.Round(m[i, j]);
           }

           dgv.DataSource = table;
       }

       private DataTable CreateTable(int rows, int columns) {
           DataTable t = new DataTable();
           for (int j = 0; j < columns; j++)
               t.Columns.Add();
           for (int i = 0; i < rows; i++)
               t.Rows.Add();

           return t;
       }

       private void SimulatedPopulation_Shown(object sender, EventArgs e) {
            if (populations.Length == 0)
                return;
            
            int year = 0;
            DataTable dt = new DataTable();
            for (int i = 0; i < populations.Length; i++) {
                dt.Columns.Add(year.ToString()+ "-й год");
                year += period;
            }

            int rowCount = populations.Max(m => m.NoRows);

            for (int i = 0; i < rowCount; i++) {
                DataRow dr = dt.Rows.Add();
                for (int j = 0; j < populations.Length; j++ )
                    if ( i < populations[j].NoRows)
                        dr[j] = Math.Round(populations[j][i, 0]);
            }
            
            dgvPopulations.DataSource = dt;

            tbperiod.Text = period.ToString();
            tbpopCount.Text = populations.Length.ToString();            
        }



        private void button1_Click(object sender, EventArgs e) {
            int index = int.Parse(tbmatrixIndex.Text);
            PrintMatrix(dgvpopMatrixes, populationMatrixes[index]);
        }




        private void bPrediction_Click(object sender, EventArgs e) {
            periodCount = int.Parse(tbperiodCount.Text);
            if (periodCount > populations.Length - 1) {
               MessageBox.Show("Период прогнозирования не должен превышать число векторов наблюдения", "Внимание", MessageBoxButtons.OK, MessageBoxIcon.Warning);
               return;
            }
            P0 = double.Parse(tbP0.Text);
            Vnu = double.Parse(tbVnu.Text);
            Ut = double.Parse(tbUt.Text);
            DemogFilterAlg df = new DemogFilterAlg(periodCount, groupCount, populations, populationMatrixes, P0, Vnu, Ut);
             x = df.Solve();
             xx = df.SolveWithFilter();
            PrintMatrixRound(dgvPrediction, x);
            PrintMatrixRound(dgvFilter, xx);
            PrintMatrixRound(dgvReal, populations[periodCount]);

        }

        private void label9_Click(object sender, EventArgs e) {

        }

        private void bGraph_Click(object sender, EventArgs e) {
            Matrix z = populations[periodCount];
            if ((object)x == null || (object)xx == null || (object)z == null)
                return;
            ChartForm graph = new ChartForm(x, xx, z);
            graph.ShowDialog();

        }



        private void bFind_Click(object sender, EventArgs e) {

            DemogFilterAlg df = new DemogFilterAlg(periodCount, groupCount, populations,populationMatrixes, P0, Vnu, Ut);
            int expCount = int.Parse(tbExpCount.Text);
            Matrix[] x = new Matrix[expCount];
            Matrix[] xx = new Matrix[expCount];
            for (int i = 0; i < expCount; i++) {
                x[i] = df.Solve();
                xx[i] = df.SolveWithFilter();
            }
            Matrix sigma1 = DemogFilterAlg.CalclDeviation(populations[periodCount], x);
            Matrix sigma2 = DemogFilterAlg.CalclDeviation(populations[periodCount], xx);
            PrintMatrix(dgvSigmaX, sigma1);
            PrintMatrix(dgvSigmaXX, sigma2);
            double s1 = 0;
            double s2 = 0;

            for (int i = 0; i < groupCount; i++) 
            {
                s1 += sigma1[i, 0];
                s2 += sigma2[i, 0];
            }
            double ef = Math.Round(s1 / s2, 2);
            tbEff.Text = ef.ToString();
        }

        private void tabPage4_Click(object sender, EventArgs e) {

        }

        private void label17_Click(object sender, EventArgs e) {

        }

        private void label15_Click(object sender, EventArgs e) {

        }

        private void dgvSigmaXX_CellContentClick(object sender, DataGridViewCellEventArgs e) {

        }

        private void dgvSigmaX_CellContentClick(object sender, DataGridViewCellEventArgs e) {

        }

        private void tbExpCount_TextChanged(object sender, EventArgs e) {

        }

        private void label16_Click(object sender, EventArgs e) {

        }

        private void label18_Click(object sender, EventArgs e) {

        }

    }
}
